package cn.itcast.scala.course04

/**
  * 主构造器和附属构造器
  * create by zhangtian on 2018/12/29
  */
object ConstructorApp {
  def main(args: Array[String]): Unit = {
    val person = new Person("zhangsan", 12) ;
    println(person.name + " : " + person.age + " : " +person.school)

    val person2 = new Person("lisi", 33, "M")
    println(person2.name + " : " + person2.age + " : " + person2.gender + " : "  +person2.school)
  }

  /**
    * 在class上的构造器为主
    * @param name
    * @param age
    */
  class Person(val name: String, val age: Int) {
    println("Person constructor enter ...")
    val school = "剑桥"
    var gender: String = _

    // 附属构造器
    def this (name: String, age: Int, gender: String){
      this(name, age) // 附属构造器的第一行代码必须要调用主构造器或其他构造器
      this.gender = gender
    }

    println("Person constructor leave ...")
  }
}
